import logging
import logging.config
import yaml
from pathlib import Path

def setup_logging(default_level=logging.INFO):
    """设置日志配置"""
    try:
        # 获取日志配置文件路径
        log_config_path = Path(__file__).parent.parent.parent / 'config' / 'logging.yaml'
        
        # 创建日志目录
        log_dir = Path(__file__).parent.parent.parent / 'logs'
        log_dir.mkdir(exist_ok=True)
        
        # 读取日志配置
        with open(log_config_path, 'r', encoding='utf-8') as f:
            config = yaml.safe_load(f)
            
            # 修正日志文件路径
            for handler in config['handlers'].values():
                if 'filename' in handler:
                    log_path = Path(handler['filename'])
                    if not log_path.is_absolute():
                        handler['filename'] = str(log_dir / log_path.name)
            
            logging.config.dictConfig(config)
            
    except Exception as e:
        print(f'Error in loading logging configuration: {e}')
        print('Using default logging configuration')
        logging.basicConfig(level=default_level)

def get_logger(name):
    """获取logger实例"""
    return logging.getLogger(name)

# 初始化日志配置
setup_logging() 